package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;


import liumengbaio.lolzhangyoubao.Activity.Adapter.MoreNovelAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreNovelDateInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreNovelInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/5.
 */
public class MoreNovelActivity extends AppCompatActivity {

    private ListView mListView;
    private MoreNovelAdapter mMoreNovelAdapter;
    private MoreNovelInfo mMoreNovelInfo;
    private MoreNovelDateInfo mMoreNovelDateInfo;
    private ImageView back;
    private String url = " http://lol.zhangyoubao.com/apis/rest/CatalogsService/all?cattype=novel&i_=" +
            "JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=" +
            "&t_=1473044488236&p_=27053&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=19&cha=360Martket&u_=" +
            "15863305&modle_=MX4&api=CatalogsService.all&time=1473044488&game=lol&secretId=" +
            "AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=682317&secretSignature=6FNjLqqFZmjBpwns0Gv2IqxMrc4=";
    private String url_date = "http://lol.zhangyoubao.com/apis/rest/CatalogsService/nlistUpdateTime?" +
            "cattype=novel&i_=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21" +
            "/wEw63JVMaX7Y3v5w=&t_=1473044488667&p_=28346&v_=400901&a_=lol&pkg_=com.anzogame.lol&" +
            "d_=android&osv_=19&cha=360Martket&u_=15863305&modle_=MX4&api=CatalogsService.nlistUpdateTime&" +
            "time=1473044488&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&" +
            "nonce=633027&secretSignature=cohNUoQ9t8C9sJHE2IR8cv0iBn8=";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_novel);
        initView();
        initEvent();
        GetFromDataWeb();
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.more_recreation_back);
        mListView = (ListView) findViewById(R.id.more_novel_listView);
    }

    private void initEvent() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MoreNovelActivity.this, MoreNovelDetailsActivity.class);
                intent.putExtra("name", mMoreNovelInfo.getData().get(0).getCatword_id().get(position).getName());
                intent.putExtra("desc", mMoreNovelInfo.getData().get(0).getCatword_id().get(position).getDesc());
                intent.putExtra("pic_url", mMoreNovelInfo.getData().get(0).getCatword_id().get(position).getPic_url());
                intent.putExtra("id", mMoreNovelInfo.getData().get(0).getCatword_id().get(position).getId());
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void GetFromDataWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request mRequest = new Request.Builder().url(url).build();
                Call call = client.newCall(mRequest);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(MoreNovelActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        mMoreNovelInfo = gson.fromJson(result, MoreNovelInfo.class);
                        if (mMoreNovelInfo.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url_date).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(MoreNovelActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        mMoreNovelDateInfo = gson.fromJson(result, MoreNovelDateInfo.class);
                        if (mMoreNovelDateInfo.getData().size() > 0) {
                            handler.sendEmptyMessage(2);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(MoreNovelActivity.this, "抱歉，没有得到数据！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    mMoreNovelAdapter = new MoreNovelAdapter(MoreNovelActivity.this, mMoreNovelInfo.getData().get(0).getCatword_id(), mMoreNovelDateInfo);
                    mListView.setAdapter(mMoreNovelAdapter);
                    break;
                case 2:
                    break;
            }
        }
    };
}
